﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace 工作流XMLDomain修改工具.Codes
{
    public class Business
    {
        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="keywords"></param>
        /// <param name="count">查询记录数</param>
        /// <returns></returns>
        public static DataTable GetDataTable(string keywords, int count)
        {
            string sql = string.Empty;
            Regex regex = new Regex(@"^[a-zA-Z0-9]{8}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{4}\-[a-zA-Z0-9]{12}$");    //GUID
            Guid gid = Guid.Empty;
            try
            {
                gid = new Guid(keywords);
            }
            catch
            {
                gid = Guid.Empty;
            }
            if (gid != Guid.Empty)
            {
                sql = @"
select top " + count.ToString() + @"  *
from    myWorkflowProcessEntity
where   ProcessGUID=@Keywords";
            }
            else
            {
                sql = @"
select top " + count.ToString() + @"  *
from    myWorkflowProcessEntity
where   ProcessName like '%' + @Keywords + '%'";
            }


            using (SqlConnection conn = 主界面.DbSvr.GetConnection())
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter[] parms = new SqlParameter[]
                {
                    new SqlParameter("Keywords",keywords)
                };
                cmd.Parameters.AddRange(parms);

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds.Tables[0];
            }
        }

        public static DataTable GetProcessData(Guid processGUID)
        {
            string sql =
                sql = @"
select top 1  *
from    myWorkflowProcessEntity
where   ProcessGUID=@ProcessGUID";

            using (SqlConnection conn = 主界面.DbSvr.GetConnection())
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter[] parms = new SqlParameter[]
                {
                    new SqlParameter("ProcessGUID",processGUID)
                };
                cmd.Parameters.AddRange(parms);

                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                return ds.Tables[0];
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="processGUID"></param>
        /// <param name="domainXML">新内容</param>
        /// <param name="oldDomainXML">旧内容，用于历史记录</param>
        public static void UpdateDomainXML(Guid processGUID, string domainXML, string oldDomainXML,string phoneFormDomainXml,string oldPhoneFormDomainXml,string phoneFormMode,string oldPhoneFormMode)
        {
            string sql = @"INSERT INTO dbo.myWorkflowProcessEntityBTDomainXMLLog
(
    ProcessGUID,
    NewXML,
    OldXML,
    NewPhoneFormDomainXml,
    OldPhoneFormDomainXml,
    NewPhoneFormMode,
    OldPhoneFormMode
)
VALUES
(@ProcessGUID, @NewXML, @OldXML, @NewPhoneFormDomainXml, @OldPhoneFormDomainXml, @NewPhoneFormMode,
 @OldPhoneFormMode);

UPDATE myWorkflowProcessEntity
SET BT_DomainXML = @NewXML,
    PhoneFormDomainXml = @NewPhoneFormDomainXml,
    PhoneFormMode = @NewPhoneFormMode
WHERE ProcessGUID = @ProcessGUID;

";  //更新

            using (SqlConnection conn = 主界面.DbSvr.GetConnection())
            {
                conn.Open();
                using (SqlTransaction trans = conn.BeginTransaction())
                {
                    SqlCommand cmd = new SqlCommand(sql, conn, trans);
                    SqlParameter[] parms = new SqlParameter[]
                    {
                        new SqlParameter("ProcessGUID",processGUID),
                        new SqlParameter("NewXML",domainXML),
                        new SqlParameter("OldXML",oldDomainXML),
                        new SqlParameter("NewPhoneFormDomainXml",phoneFormDomainXml),
                        new SqlParameter("OldPhoneFormDomainXml",oldPhoneFormDomainXml),
                        new SqlParameter("NewPhoneFormMode",phoneFormMode),
                        new SqlParameter("OldPhoneFormMode",oldPhoneFormMode),
                    };
                    cmd.Parameters.AddRange(parms);
                    cmd.ExecuteNonQuery();

                    trans.Commit();
                }
            }
        }
    }
}
